package com.tfw.domain;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @program: fruit-sale
 * @description: 优惠券
 * @author: Fuwen
 * @create: 2022-07-27 18:15
 **/
public class Coupons extends Preferential{

    /**
     * 最少金额
     */
    private BigDecimal lowPrice;
    /**
     * 减少金额
     */
    private BigDecimal CouPrice;

    /**
     * 使用优惠
     * @param goods
     * @return
     */
    @Override
    public BigDecimal useCou(List<Good> goods){
        List<Good> couList = getGoods(goods);
        //非打折列表
        couList.forEach(goods::remove);
        BigDecimal decimal = BigDecimal.ZERO;
        if (goods.size()!=0){
            decimal=decimal.add(basePrice(goods));
        }
        BigDecimal bigDecimal1=BigDecimal.ZERO;
        if (couList.size()!=0){
            bigDecimal1=bigDecimal1.add(basePrice(couList));
            if ((bigDecimal1.compareTo(lowPrice)>0)){
                bigDecimal1 = bigDecimal1.subtract(CouPrice.multiply((bigDecimal1.divide(lowPrice,0, RoundingMode.DOWN))));
            }
        }
        goods.addAll(couList);
        return decimal.add(bigDecimal1);
    }

    /**
     * 区分打折与非打折
     * @param goods
     * @return
     */
    private List<Good> getGoods(List<Good> goods) {
        //打折列表
        List<Good> couList = new ArrayList<>();
        boolean b=Objects.isNull(this.getBeginDate())&&Objects.isNull(this.getEndDate());
        for (Good good: goods){
            if (!b&&this.getGoodId().contains(good.getUuid())&&this.getBeginDate().compareTo(new Date())<0
                    &&this.getEndDate().compareTo(new Date())>0){
                if (!Objects.isNull(lowPrice)&&!Objects.isNull(CouPrice)){
                    couList.add(good);
                }
            }
        }
        return couList;
    }

    public BigDecimal getLowPrice() {
        return lowPrice;
    }

    public void setLowPrice(BigDecimal lowPrice) {
        this.lowPrice = lowPrice;
    }

    public BigDecimal getCouPrice() {
        return CouPrice;
    }

    public void setCouPrice(BigDecimal couPrice) {
        CouPrice = couPrice;
    }
}
